package com.xidian.yunwudemo.service.impl;

import com.xidian.yunwudemo.dto.DataViewAdaptiveDto;
import com.xidian.yunwudemo.entities.PieBasicEntity;
import com.xidian.yunwudemo.mapper.master.DataAdaptiveMapper;
import com.xidian.yunwudemo.service.DataViewAdaptiveService;
import com.xidian.yunwudemo.util.AuthWayUtil;
import com.xidian.yunwudemo.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class DataViewAdaptiveServiceImpl implements DataViewAdaptiveService {
    @Autowired
    DataAdaptiveMapper dataAdaptiveMapper;

    @Override
    public DataViewAdaptiveDto getAdaptiveDayData() {
        Date dNow = new Date();
        String[] timeScope = TimeUtil.TIME_UTIL.OneDay(dNow);
        List<Map<String,Integer>> sqlRes = dataAdaptiveMapper.getScopeData(timeScope[0],timeScope[1]);
        return dataFormat(sqlRes);
    }

    @Override
    public DataViewAdaptiveDto getAdaptiveWeekData() {
        Date dNow = new Date();
        String[] timeScope = TimeUtil.TIME_UTIL.OneWeek(dNow);
        List<Map<String,Integer>> sqlRes = dataAdaptiveMapper.getScopeData(timeScope[0],timeScope[1]);
        return dataFormat(sqlRes);
    }

    @Override
    public DataViewAdaptiveDto getAdaptiveAllData() {
        List<Map<String,Integer>> sqlRes = dataAdaptiveMapper.getAllTimeData();
        return dataFormat(sqlRes);
    }

    /**
     *
     * @param arg
     * @return DataViewAdaptiveDto
     * 此处要求每个数据都必须有
     * TODO: 自定义判断填充
     * authWay:
     * 1:EPS-AKA(4G)
     * 2:5G-AKA
     * 3:EAP-AKA(4G)
     * 4:群组认证
     * 7: 自适应EPS-AKA
     * 8: 自适应5G-AKA
     * 9: 自适应的EAP-AKA’
     * 10: 自适应的群组认证
     */
    private DataViewAdaptiveDto dataFormat(List<Map<String,Integer>> arg){
        int sz = arg.size();
        PieBasicEntity[] pie = new PieBasicEntity[8];
        // count 存在问题
        int[] count = new int[8];
        String[] dist = new String[]{"EPS-AKA","5G-AKA","EAP-AKA'","群组认证","自适应EPS-AKA","自适应5G-AKA","自适应EAP-AKA'","自适应群组认证"};
        // auth_way的索引映射，如authw_way=1,其存储对应的index为0
        // pie初始化
        for(int i = 0 ; i < 8; i++){
            pie[i] = new PieBasicEntity(0,dist[i]);
        }

        int authWay = 0, authCount = 0, index = 0;
        for(int i = 0 ; i < sz; i++){
            authWay = arg.get(i).get("authWay");
            authCount =arg.get(i).get("authCount");
            // 其authWay还包括其他如5,6选项
            if(!AuthWayUtil.ADAPTIVE.containsKey(authWay)) continue;
            index = AuthWayUtil.ADAPTIVE.get(authWay);
            pie[index].setValue(authCount);
            // count赋值
            count[index] = authCount;
        }
        return new DataViewAdaptiveDto(count,pie);
    }
}
